From 823ab3fd0c66c0829ccf537cf7955b107a2350f0 Mon Sep 17 00:00:00 2001 From: Ewan Mellor Date: Tue, 27 Mar 2007 22:34:22 +0100 Subject: [PATCH] Move the client-specific parts of xmlrpclib2 into xmlrpcclient.py. This means that clients do not need to import swathes of server code. Signed-off-by: Ewan Mellor --- tools/python/xen/util/xmlrpcclient.py | 94 +++++++++++++++++++++++++++ tools/python/xen/util/xmlrpclib2.py | 70 -------------------- tools/python/xen/xend/XendClient.py | 2 +- tools/python/xen/xm/XenAPI.py | 13 ++-- tools/python/xen/xm/main.py | 2 +- 5 files changed, 102 insertions(+), 79 deletions(-) create mode 100644 tools/python/xen/util/xmlrpcclient.py diff --git a/tools/python/xen/util/xmlrpcclient.py b/tools/python/xen/util/xmlrpcclient.py new file mode 100644 index 0000000000..ec12c62f27 --- /dev/null +++ b/tools/python/xen/util/xmlrpcclient.py @@ -0,0 +1,94 @@ +#============================================================================ +# This library is free software; you can redistribute it and/or +# modify it under the terms of version 2.1 of the GNU Lesser General Public +# License as published by the Free Software Foundation. +# +# This library is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +# Lesser General Public License for more details. +# +# You should have received a copy of the GNU Lesser General Public +# License along with this library; if not, write to the Free Software +# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +#============================================================================ +# Copyright (C) 2006 Anthony Liguori +# Copyright (C) 2007 XenSource Inc. +#============================================================================ + + +from httplib import FakeSocket, HTTPConnection, HTTP +import socket +import xmlrpclib +from types import StringTypes + + +try: + import SSHTransport + ssh_enabled = True +except ImportError: + # SSHTransport is disabled on Python <2.4, because it uses the subprocess + # package. + ssh_enabled = False + + + +# A new ServerProxy that also supports httpu urls. An http URL comes in the +# form: +# +# httpu:///absolute/path/to/socket.sock +# +# It assumes that the RPC handler is /RPC2. This probably needs to be improved + +class HTTPUnixConnection(HTTPConnection): + def connect(self): + self.sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) + self.sock.connect(self.host) + +class HTTPUnix(HTTP): + _connection_class = HTTPUnixConnection + +class UnixTransport(xmlrpclib.Transport): + def request(self, host, handler, request_body, verbose=0): + self.__handler = handler + return xmlrpclib.Transport.request(self, host, '/RPC2', + request_body, verbose) + def make_connection(self, host): + return HTTPUnix(self.__handler) + + +# See xmlrpclib2.TCPXMLRPCServer._marshalled_dispatch. +def conv_string(x): + if isinstance(x, StringTypes): + s = string.replace(x, "'", r"\047") + exec "s = '" + s + "'" + return s + else: + return x + + +class ServerProxy(xmlrpclib.ServerProxy): + def __init__(self, uri, transport=None, encoding=None, verbose=0, + allow_none=1): + if transport == None: + (protocol, rest) = uri.split(':', 1) + if protocol == 'httpu': + uri = 'http:' + rest + transport = UnixTransport() + elif protocol == 'ssh': + global ssh_enabled + if ssh_enabled: + (transport, uri) = SSHTransport.getHTTPURI(uri) + else: + raise ValueError( + "SSH transport not supported on Python <2.4.") + xmlrpclib.ServerProxy.__init__(self, uri, transport, encoding, + verbose, allow_none) + + def __request(self, methodname, params): + response = xmlrpclib.ServerProxy.__request(self, methodname, params) + + if isinstance(response, tuple): + return tuple([conv_string(x) for x in response]) + else: + return conv_string(response) diff --git a/tools/python/xen/util/xmlrpclib2.py b/tools/python/xen/util/xmlrpclib2.py index de2b16e469..edf0720114 100644 --- a/tools/python/xen/util/xmlrpclib2.py +++ b/tools/python/xen/util/xmlrpclib2.py @@ -26,7 +26,6 @@ import fcntl from types import * -from httplib import HTTPConnection, HTTP from SimpleXMLRPCServer import SimpleXMLRPCServer, SimpleXMLRPCRequestHandler import SocketServer import xmlrpclib, socket, os, stat @@ -36,14 +35,6 @@ import mkdir from xen.web import connection from xen.xend.XendLogging import log -try: - import SSHTransport - ssh_enabled = True -except ImportError: - # SSHTransport is disabled on Python <2.4, because it uses the subprocess - # package. - ssh_enabled = False - # # Convert all integers to strings as described in the Xen API # @@ -64,13 +55,6 @@ def stringify(value): return value -# A new ServerProxy that also supports httpu urls. An http URL comes in the -# form: -# -# httpu:///absolute/path/to/socket.sock -# -# It assumes that the RPC handler is /RPC2. This probably needs to be improved - # We're forced to subclass the RequestHandler class so that we can work around # some bugs in Keep-Alive handling and also enabled it by default class XMLRPCRequestHandler(SimpleXMLRPCRequestHandler): @@ -106,60 +90,6 @@ class XMLRPCRequestHandler(SimpleXMLRPCRequestHandler): if self.close_connection == 1: self.connection.shutdown(1) -class HTTPUnixConnection(HTTPConnection): - def connect(self): - self.sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) - self.sock.connect(self.host) - -class HTTPUnix(HTTP): - _connection_class = HTTPUnixConnection - -class UnixTransport(xmlrpclib.Transport): - def request(self, host, handler, request_body, verbose=0): - self.__handler = handler - return xmlrpclib.Transport.request(self, host, '/RPC2', - request_body, verbose) - def make_connection(self, host): - return HTTPUnix(self.__handler) - - -# See _marshalled_dispatch below. -def conv_string(x): - if isinstance(x, StringTypes): - s = string.replace(x, "'", r"\047") - exec "s = '" + s + "'" - return s - else: - return x - - -class ServerProxy(xmlrpclib.ServerProxy): - def __init__(self, uri, transport=None, encoding=None, verbose=0, - allow_none=1): - if transport == None: - (protocol, rest) = uri.split(':', 1) - if protocol == 'httpu': - uri = 'http:' + rest - transport = UnixTransport() - elif protocol == 'ssh': - global ssh_enabled - if ssh_enabled: - (transport, uri) = SSHTransport.getHTTPURI(uri) - else: - raise ValueError( - "SSH transport not supported on Python <2.4.") - xmlrpclib.ServerProxy.__init__(self, uri, transport, encoding, - verbose, allow_none) - - def __request(self, methodname, params): - response = xmlrpclib.ServerProxy.__request(self, methodname, params) - - if isinstance(response, tuple): - return tuple([conv_string(x) for x in response]) - else: - return conv_string(response) - - # This is a base XML-RPC server for TCP. It sets allow_reuse_address to # true, and has an improved marshaller that logs and serializes exceptions. diff --git a/tools/python/xen/xend/XendClient.py b/tools/python/xen/xend/XendClient.py index ef727c2676..ef16699c4b 100644 --- a/tools/python/xen/xend/XendClient.py +++ b/tools/python/xen/xend/XendClient.py @@ -17,7 +17,7 @@ # Copyright (C) 2006 Anthony Liguori #============================================================================ -from xen.util.xmlrpclib2 import ServerProxy +from xen.util.xmlrpcclient import ServerProxy import os import sys diff --git a/tools/python/xen/xm/XenAPI.py b/tools/python/xen/xm/XenAPI.py index 026b5ec69b..74be43cbb3 100644 --- a/tools/python/xen/xm/XenAPI.py +++ b/tools/python/xen/xm/XenAPI.py @@ -12,7 +12,7 @@ # License along with this library; if not, write to the Free Software # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA #============================================================================ -# Copyright (C) 2006 XenSource Inc. +# Copyright (C) 2006-2007 XenSource Inc. #============================================================================ # # Parts of this file are based upon xmlrpclib.py, the XML-RPC client @@ -47,7 +47,7 @@ import gettext import xmlrpclib -import xen.util.xmlrpclib2 +import xen.util.xmlrpcclient as xmlrpcclient translation = gettext.translation('xen-xm', fallback = True) @@ -85,7 +85,7 @@ class Failure(Exception): _RECONNECT_AND_RETRY = (lambda _ : ()) -class Session(xen.util.xmlrpclib2.ServerProxy): +class Session(xmlrpcclient.ServerProxy): """A server proxy and session manager for communicating with Xend using the Xen-API. @@ -104,9 +104,8 @@ class Session(xen.util.xmlrpclib2.ServerProxy): def __init__(self, uri, transport=None, encoding=None, verbose=0, allow_none=1): - xen.util.xmlrpclib2.ServerProxy.__init__(self, uri, transport, - encoding, verbose, - allow_none) + xmlrpcclient.ServerProxy.__init__(self, uri, transport, encoding, + verbose, allow_none) self._session = None self.last_login_method = None self.last_login_params = None @@ -153,7 +152,7 @@ class Session(xen.util.xmlrpclib2.ServerProxy): elif name.startswith('login'): return lambda *params: self._login(name, params) else: - return xen.util.xmlrpclib2.ServerProxy.__getattr__(self, name) + return xmlrpcclient.ServerProxy.__getattr__(self, name) def _parse_result(result): diff --git a/tools/python/xen/xm/main.py b/tools/python/xen/xm/main.py index d4e0191514..87f5ae29a8 100644 --- a/tools/python/xen/xm/main.py +++ b/tools/python/xen/xm/main.py @@ -49,7 +49,7 @@ from xen.xend.XendConstants import * from xen.xm.opts import OptionError, Opts, wrap, set_true from xen.xm import console -from xen.util.xmlrpclib2 import ServerProxy +from xen.util.xmlrpcclient import ServerProxy import XenAPI -- 2.30.2